
package net.swa.dewei.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.stereotype.Service;
import net.swa.dewei.beans.entity.LifeCardCar;
import net.swa.dewei.beans.vo.CardCarItems;
import net.swa.dewei.service.LifeCardCarService;
import net.swa.system.dao.HibernateDaoSupport;
import net.swa.util.BuzConst;
import net.swa.util.DateUtils;

@Service("lifeCardCarService")
public class LifeCardCarServiceImpl extends HibernateDaoSupport implements LifeCardCarService
{
    private static final Logger log = Logger.getLogger(LifeCardCarServiceImpl.class);

    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> addToCar(Long goodId , Integer buyNum , String wxid)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", false);
        map.put("message", "商品添加失败");
        Session session = getCurrentSession();
        Query query = session.createQuery("from  LifeCardCar  where wxid=:wxid and goodsId=:goodId");
        query.setString("wxid", wxid);
        query.setLong("goodId", goodId);
        List<LifeCardCar> list = query.list();
        if (null != list && list.size() > 0)
        {
            LifeCardCar m = list.get(0);
            m.setNum(m.getNum() + buyNum);
            m.setUpdateDate(DateUtils.getCurrDate(null));
            session.update(m);
        }
        else
        {
            LifeCardCar m = new LifeCardCar();
            m.setNum(buyNum);
            m.setGoodsId(goodId);
            m.setWxid(wxid);
            m.setCreateDate(DateUtils.getCurrDate(null));
            session.save(m);
        }
        map.put("success", true);
        map.put("message", "");
        return map;
    }

    @SuppressWarnings("unchecked")
    @Override
    public CardCarItems queryItems(String wxid)
    {
        CardCarItems its = new CardCarItems();
        Session session = getCurrentSession();
        String sql = "SELECT c.id,c.goodsId,c.num,c.wxid,c.createDate,c.updateDate,g.`name`,g.cardprice,g.imgUrl from t_life_car c join t_life_goods g on g.id=c.goodsId  where c.wxid=:wxid";
        Query query = session.createSQLQuery(sql).addScalar("id", StandardBasicTypes.LONG).addScalar("goodsId", StandardBasicTypes.LONG).addScalar("num", StandardBasicTypes.INTEGER).addScalar("wxid", StandardBasicTypes.STRING).addScalar("createDate", StandardBasicTypes.STRING).addScalar("updateDate", StandardBasicTypes.STRING).addScalar("name", StandardBasicTypes.STRING).addScalar("cardprice", StandardBasicTypes.DOUBLE).addScalar("imgUrl", StandardBasicTypes.STRING).setResultTransformer(
                Transformers.aliasToBean(LifeCardCar.class));
        query.setString("wxid", wxid);
        List<LifeCardCar> list = query.list();
        if (null != list && list.size() > 0)
        {
            List<LifeCardCar> l = new ArrayList<LifeCardCar>();
            double total = 0d;
            for (LifeCardCar o : list)
            {
                if (o.getCardprice() > 0 && o.getNum() > 0)
                {
                    o.setTotalMony(BuzConst.mul(o.getCardprice(), o.getNum()));
                    total = BuzConst.add(o.getTotalMony(), total);
                    l.add(o);
                }
                else
                {
                    log.debug("购物车信息有误");
                }
            }
            its.setItems(l);
            its.setAllTotalMoney(total);
        }
        return its;
    }

    @Override
    public boolean updateBuyNum(Integer buyNum , Long id)
    {
        Session session = getCurrentSession();
        String hql = "update LifeCardCar set num=:num where id=:id";
        Query q = session.createQuery(hql);
        q.setInteger("num", buyNum);
        q.setLong("id", id);
        q.executeUpdate();
        return true;
    }

    @Override
    public boolean deleteItem(Long id)
    {
        Session session = getCurrentSession();
        LifeCardCar it = (LifeCardCar) session.get(LifeCardCar.class, id);
        if (null != it)
        {
            session.delete(it);
            return true;
        }
        return false;
    }
}